package com.example.administrator.qimo3no74;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.qimo3no74.orders.OrdersItem;
import com.example.administrator.qimo3no74.orders.OrdersItemAdapter;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class OrdersActivity extends AppCompatActivity {
    private List<OrdersItem> ordersItems = new ArrayList<>();
    private Toolbar toolbar;
    private TextView toolbarTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orders);
        toolbar = findViewById(R.id.toolbar3);
        toolbarTitle=toolbar.findViewById(R.id.toolbar_title3);


        //载入订单
        intiOrdersItem();
        actionOrdersItem();

        //标题栏
        addToolbar();


    }

    private void actionOrdersItem() {
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.ordersRecyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        OrdersItemAdapter adapter = new OrdersItemAdapter(ordersItems);
        recyclerView.setAdapter(adapter);
    }

    private void intiOrdersItem(){
        ordersItems.add(new OrdersItem("毛呢背心裙秋冬女2018新款修身显瘦中长款背带无袖打底马甲A字裙",129.00,2,"百魅女人屋",R.drawable.beixin_qun1,"毛呢背心裙秋冬女2018新款修身显瘦中长款背带无袖打底马甲A字裙"));
        ordersItems.add(new OrdersItem("抹茶故事工装棉服男中长款2018新款棉衣韩版潮流棉袄ins冬季外套",388.00,1,"抹茶故事旗舰店",R.drawable.mocha_gushi,"抹茶故事工装棉服男中长款2018新款棉衣韩版潮流棉袄ins冬季外套"));
        ordersItems.add(new OrdersItem("骆驼男装 2018秋冬新款青年纯色中长款加厚外套可脱卸帽羽绒服男",1198.00,1,"骆驼男装旗舰店",R.drawable.luotou_nan,"骆驼男装 2018秋冬新款青年纯色中长款加厚外套可脱卸帽羽绒服男"));
        ordersItems.add(new OrdersItem("毛呢背心裙秋冬女2018新款修身显瘦中长款背带无袖打底马甲A字裙",129.00,5,"百魅女人屋",R.drawable.beixin_qun1,"毛呢背心裙秋冬女2018新款修身显瘦中长款背带无袖打底马甲A字裙"));
        ordersItems.add(new OrdersItem("毛呢背心裙秋冬女2018新款修身显瘦中长款背带无袖打底马甲A字裙",129.00,1,"百魅女人屋",R.drawable.beixin_qun1,"毛呢背心裙秋冬女2018新款修身显瘦中长款背带无袖打底马甲A字裙"));


    }

    private void addToolbar() {
        toolbar.setNavigationIcon(R.drawable.ic_chevron_left_black_24dp);
        toolbar.setTitle("");
        toolbarTitle.setText("全部订单");
        toolbar.inflateMenu(R.menu.menu_orders);//设置右上角的填充菜单
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_orders, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id){
            case  R.id.menu_search :
                Toast.makeText(this,"搜索更多",Toast.LENGTH_SHORT).show();
                break;
            case  R.id.menu_messages:
                Toast.makeText(this,"信息通知",Toast.LENGTH_SHORT).show();
                break;
            case  R.id.menu_home :
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                break;
            case R.id.menu_kefu:
                Toast.makeText(this, "我的客服", Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_feedback:
                Toast.makeText(this, "我要反馈", Toast.LENGTH_SHORT).show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    // 让菜单同时显示图标和文字
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return super.onMenuOpened(featureId, menu);
    }

}
